<?php
/**
 * Permission Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\questions;

use app\admin\model\questions\Question as QuestionModels;
use think\Db;
use app\admin\model\questions\Answer;

Class Question {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];

        if ( get($get, 'title') ) {
            $map[] = ['title', 'LIKE', '%'.$get['title'].'%'];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;

        return QuestionModels::with(['questionCategory'=>function($query){
            $query->with(['parents'=>function($query){
                $query->with(['parents']);
            }]);
        }])->where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 新增优惠券信息
     * @param array $data 数据包
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $question = QuestionModels::create($data, ['title','recommend','examination_type','examination_section','examination_discipline','examination_area','cover','category']);
        return $question->isEmpty()?false:$question->id;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @return boolean
     */
    public function destory( $data )
    {
        Db::startTrans();
        if ( is_array( $data ) ) {
            foreach ( $data as $_data ) {
                $opt = Answer::get(function($query)use($_data){
                    $query->where('qid', '=', $_data);
                }, 'opt');
                if (false === $opt->together('opt')->delete()) {
                    Db::rollback();
                    return false;
                }
            }
        } else {
            $opt = Answer::get(function($query)use($data){
                $query->where('qid', '=', $data);
            }, 'opt');
            if (false === $opt->together('opt')->delete()) {
                Db::rollback();
                return false;
            }
        }
        if ( false === QuestionModels::destroy(function($query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        })){
            Db::rollback();
            return false;
        }
        Db::commit();
        return true;
    }

    /**
     * 优惠券管理编辑
     * @param QuestionModels $model
     * @param array $data
     */
    public function save(QuestionModels $model, array $data)
    {
        $model->title = $data['title'];
        $model->recommend = $data['recommend'];
        $model->examination_type = $data['examination_type'];
        $model->examination_section = $data['examination_section'];
        $model->examination_discipline = $data['examination_discipline'];
        $model->examination_area = $data['examination_area'];
        $model->cover = $data['cover'];
        $model->category = $data['category'];
        return $model->save();
    }

    /**
     * 获取试题分组集合
     * @return array
     */
    public function getList()
    {
        return QuestionModels::field('id,title')->select()->toArray();
    }

}